from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template, redirect_to
from django.views.static import serve as static_serve

import kangapy4.blog.views as kpview

# Regexp for an optional category name
catNamePat = r'(?:(?P<catName>[a-z_]+)/)'
yearPat = r'(?P<year>\d\d\d\d)'
monthPat = yearPat + r'[-_](?P<month>\d\d)'
dayPat = monthPat + r'[-_](?P<day>\d\d)'
slugPat = r'(?P<slug>[^./]+)(?:\.html)?'

urlpatterns = patterns('',

    # Site index
    (r'^$', direct_to_template, dict(template='blog/site_index.html')),

    # Documents
    # URLs in Kangapy3 were /docs/howto_estimate.html
    # Now migrating to /docs/howto-estimate/
    (r'^docs/$', redirect_to, dict(url='/docs/index.html')),
    (r'^docs/' + slugPat + r'/?$', kpview.show_doc),

    # Blog Entries - Index page
    (r'^blog/$', redirect_to, dict(url='/blog/index.html')),
    (r'^blog/' + catNamePat + r'$', redirect_to,
        dict(url='/blog/%(catName)s/index.html')),
    (r'^blog/' + catNamePat + '?' + r'index.html/?$', kpview.blog_index),

    # Yearly archives
    (r'^blog/' + yearPat + r'(?:\.html)?/?$', redirect_to, 
        dict(url='/blog/%(year)s/index.html')),
    (r'^blog/' + catNamePat + yearPat + r'(?:\.html)?/?$', redirect_to, 
        dict(url='/blog/%(catName)/%(year)s/index.html')),
    (r'^blog/' + catNamePat + '?' + yearPat + r'/index.html/?$', kpview.blog_year),

    # Monthly archives
    (r'^blog/' + monthPat + r'(?:\.html)?/?$', redirect_to, 
        dict(url='/blog/%(year)s-%(month)s/index.html')),
    (r'^blog/' + catNamePat + monthPat + r'(?:\.html)?/?$', redirect_to, 
        dict(url='/blog/%(catName)/%(year)s-%(month)s/index.html')),
    (r'^blog/' + catNamePat + '?' + monthPat + r'/index.html/?$', kpview.blog_month),

    # Daily archives
    (r'^blog/' + dayPat + r'(?:\.html)?/?$', redirect_to, 
        dict(url='/blog/%(year)s-%(month)s-%(day)s/index.html')),
    (r'^blog/' + catNamePat + dayPat + r'(?:\.html)?/?$', redirect_to, 
        dict(url='/blog/%(catName)/%(year)s-%(month)s-%(day)s/index.html')),
    (r'^blog/' + catNamePat + '?' + dayPat + r'/index.html/?$', kpview.blog_day),

    # Single archive page  
    # XXX Catch MT- style URLs here
    (r'^blog/' +  dayPat + r'/' + slugPat + r'/?$', kpview.blog_entry),

    # Admin site
    (r'^admin/', include('django.contrib.admin.urls')),
    (r'^r/',     include( 'django.conf.urls.shortcut' )),

    # Media files (only in development)
    (r'^styles/(.*)$', static_serve, dict(document_root='files/styles')),
)
